package com.yeqing.iodemo;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 在 FileDemo 中学习了创建、查找、删除，但是还无法读取、传输文件中的内容
 * IO 流主要是读取、传输、写入数据内容
 */
public class IODemo {

    public static void read方法读取() throws IOException {
        File file = new File("/Users/cyq/Develop/三国/张飞.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        // 核心代码
        int b;
        while ((b = fileInputStream.read()) != -1) {
            System.out.println((char) b);
        }
        fileInputStream.close();
    }

    public static void read方法读取字节数组() throws IOException {
        File file = new File("/Users/cyq/Develop/三国/张飞.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        // 核心代码
        byte[] bytes = new byte[3];
        while(fileInputStream.read(bytes) != -1) {
            System.out.println(new String(bytes));
        }

        // 输出内容：
        // wos
        // hiz
        // han
        // gfe
        // ife
        // 由于最后一次读取时，只读取一个字节i，数组中还是上次的数据 gfe，只替换了i，所以最后输出了 ife
    }

    /**
     * 使用FileInputStream读取的正确姿势
     * @throws IOException
     */
    public static void read方法读取字节数组2() throws IOException {
        File file = new File("/Users/cyq/Develop/三国/张飞.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        // 核心代码
        byte[] bytes = new byte[3];
        int len;
        while((len = fileInputStream.read(bytes)) != -1) {
            // len 为每次读取的有效字节个数
            System.out.println(new String(bytes, 0, len));
        }
    }

    /**
     * 用 fileoutputstream创建文件
     * @throws FileNotFoundException
     */
    public static void newFile() throws FileNotFoundException {
        File file = new File("/Users/cyq/Develop/三国/张飞2.txt");
        FileOutputStream fileOutputStream1 = new FileOutputStream(file);
        FileOutputStream fileOutputStream2 = new FileOutputStream("/Users/cyq/Develop/三国/司马懿.txt");
    }

    /**
     * 写入文件
     * @throws FileNotFoundException
     */
    public static void writeFile() throws IOException {
        File file = new File("/Users/cyq/Develop/三国/张飞2.txt");
        FileOutputStream fileOutputStream1 = new FileOutputStream(file);
        fileOutputStream1.write("张飞 2".getBytes());
        // 输出:张飞 2

        FileOutputStream fileOutputStream2 = new FileOutputStream("/Users/cyq/Develop/三国/司马懿.txt");
        fileOutputStream2.write(96);
        fileOutputStream2.write(97);
        fileOutputStream2.write(98);
        // 输出:`ab
    }

    /**
     * 写入文件
     * @throws FileNotFoundException
     */
    public static void writeFileAppend() throws IOException {
        File file = new File("/Users/cyq/Develop/三国/张飞2.txt");
        FileOutputStream fileOutputStream1 = new FileOutputStream(file, true);
        fileOutputStream1.write("张飞 2".getBytes());
        // 输出:张飞 2张飞 2

        FileOutputStream fileOutputStream2 = new FileOutputStream("/Users/cyq/Develop/三国/司马懿.txt", true);
        fileOutputStream2.write(96);
        fileOutputStream2.write(97);
        fileOutputStream2.write(98);
        // 输出:`ab`ab
    }

    /**
     * 复制文件
     * @param file
     * @param copyFile
     * @throws IOException
     */
    public static void copyFile(File file, File copyFile) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);

        FileOutputStream fileOutputStream = new FileOutputStream(copyFile, true);

        byte[] data = new byte[1024];
        int len;
        while ((len = fileInputStream.read(data)) != -1) {
            fileOutputStream.write(data, 0, len);
        }
    }

    public static void 内存节点流() {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("data".getBytes());
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int len = 0;
        while ((len = byteArrayInputStream.read()) != -1) {
            byteArrayOutputStream.write(len);
        }

        // 输出 data
        System.out.println(new String(byteArrayOutputStream.toByteArray()));
    }

    public static void fileReaderRead() throws IOException {
        FileReader fileReader = new FileReader("/Users/cyq/Develop/三国/张飞2.txt");
        int b;

        while ((b = fileReader.read()) != -1) {
            System.out.println((char) b);
        }
    }

    // 两个字符依次读取
    public static void fileReaderReadByte() throws IOException {
        FileReader fileReader = new FileReader("/Users/cyq/Develop/三国/张飞2.txt");
        int len;
        char[] data = new char[2];
        while ((len = fileReader.read(data)) != -1) {
            System.out.println(new String(data, 0, len));
        }
    }

    public static void fileWriter() throws IOException {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter("/Users/cyq/Develop/三国/孙权.txt");
            fileWriter.write("孙权");
            fileWriter.append("换牌");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileWriter != null) {
                fileWriter.close();
            }
        }
    }

    public static void 内存节点流_StringWriter() {
        StringWriter stringWriter = new StringWriter();
        stringWriter.write("hello");

        StringBuffer buffer = stringWriter.getBuffer();
        System.out.println(buffer);
    }

    public static void main(String[] args) throws IOException {
//        read方法读取();

//        read方法读取字节数组();
        // 使用字节读取，每次读取多个字节，减少了系统间的 io操作次数，从而提高了效率，建议使用
//        read方法读取字节数组2();

        // 创建文件
//        newFile();

        // 写入文件
//        writeFile();
        // 追加写入
//        writeFileAppend();

        // 复制文件
//        copyFile(new File("/Users/cyq/Develop/三国/张飞2.txt"), new File("/Users/cyq/Develop/三国/张飞3.txt"));

        // 内存节点流示例代码
//        内存节点流();

        // fileReaderRead
//        fileReaderRead();

//        fileWriter();

        内存节点流_StringWriter();
    }

}
